package com.gpframework.module.controller.module;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.lang.TypeReference;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gpframework.common.annotation.Log;
import com.gpframework.common.core.controller.BaseController;
import com.gpframework.common.core.domain.AjaxResult;
import com.gpframework.common.core.page.TableDataInfo;
import com.gpframework.common.enums.BusinessType;
import com.gpframework.common.enums.YesNoEnum;
import com.gpframework.common.utils.poi.ExcelUtil;
import com.gpframework.module.manufacture.domain.GpWeightReductionRuleSetting;
import com.gpframework.module.manufacture.service.IGpWeightReductionRuleSettingService;
import com.gpframework.module.module.domain.GpGramWeight;
import com.gpframework.module.module.domain.req.GpGramWeightAdd;
import com.gpframework.module.module.domain.req.GpGramWeightReq;
import com.gpframework.module.module.domain.req.GpGramWeightUpdate;
import com.gpframework.module.module.domain.vo.GpGramWeightVo;
import com.gpframework.module.module.service.IGpGramWeightService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;
/**
 * @ClassName GpGramWeightController
 * @Author gpframework
 * @Date 2023-12-11
 * @Version 1.0.0
 * @Description 克重Controller
 */
@Validated
@Api(tags = "克重")
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@SuppressWarnings({"rawtypes","unchecked"})
@RequestMapping("/module/gramweight" )
public class GpGramWeightController extends BaseController {

    private final IGpGramWeightService iGpGramWeightService;
    private final IGpWeightReductionRuleSettingService iGpWeightReductionRuleSettingService;

 /**
  * @Description 分页
  * @Author gpframework
  * @Date 2023-12-11
  * @Param gpGramWeight
  * @Return TableDataInfo
 */
    @ApiOperation("列表")
    @PreAuthorize("@ss.hasPermi('module:gramweight:list')")
    @GetMapping("/list")
    public TableDataInfo<GpGramWeightVo> list(GpGramWeightReq gpGramWeightReq)
    {
        startPage();
        QueryWrapper<GpGramWeight> lqw = new QueryWrapper<GpGramWeight>();
        gpGramWeightReq.generatorQuery(lqw,true);
        List<GpGramWeightVo> list = iGpGramWeightService.listVo(lqw);
        return getDataTable(list);
    }

    /**
     * @Description 新增
     * @Author gpframework
     * @Date 2023-12-11
     * @Param gpGramWeight
     * @Return TableDataInfo
     */
    @ApiOperation("新增")
    @PreAuthorize("@ss.hasPermi('module:gramweight:add')" )
    @Log(title = "添加克重" , businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Valid @RequestBody GpGramWeightAdd gpGramWeightAdd) {
        GpGramWeight gpGramWeight = Convert.convert(new TypeReference<GpGramWeight>() {}, gpGramWeightAdd);
        GpWeightReductionRuleSetting one = iGpWeightReductionRuleSettingService.getOne(new LambdaQueryWrapper<GpWeightReductionRuleSetting>().eq(GpWeightReductionRuleSetting::getProductName, gpGramWeightAdd.getProductName()).eq(GpWeightReductionRuleSetting::getDelStatus, YesNoEnum.NO.getKey()));
        if (one!=null){
            gpGramWeight.setStandardWeight(new BigDecimal(one.getStandardWeight()));
        }
        return toAjax(iGpGramWeightService.save(gpGramWeight) ? 1 : 0);
    }

    /**
     * @Description 修改
     * @Author gpframework
     * @Date 2023-12-11
     * @Param gpGramWeight
     * @Return TableDataInfo
     */
    @ApiOperation("修改")
    @PreAuthorize("@ss.hasPermi('module:gramweight:edit')" )
    @Log(title = "修改克重" , businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Valid @RequestBody List<GpGramWeightUpdate> gpGramWeightUpdateList) {
        List<GpGramWeight> list = Convert.convert(new TypeReference<List<GpGramWeight>>() {}, gpGramWeightUpdateList);
        return toAjax(iGpGramWeightService.updateBatchById(list) ? 1 : 0);
    }


    /**
     * @Description 导出
     * @Author gpframework
     * @Date 2023-12-11
     * @Param gpGramWeight
     * @Return TableDataInfo
     */

    @PreAuthorize("@ss.hasPermi('module:gramweight:export')" )
    @Log(title = "导出克重" , businessType = BusinessType.EXPORT)
    @GetMapping("/export" )
    public AjaxResult export(GpGramWeightReq gpGramWeightReq) {
        QueryWrapper<GpGramWeight> lqw = new QueryWrapper<GpGramWeight>();
        gpGramWeightReq.generatorQuery(lqw,true);
        List<GpGramWeightVo> list = iGpGramWeightService.listVo(lqw);
        ExcelUtil<GpGramWeightVo> util = new ExcelUtil<GpGramWeightVo>(GpGramWeightVo. class);
        return util.exportExcel(list, "gramweight" );
    }

    /**
     * @Description 详情
     * @Author gpframework
     * @Date 2023-12-11
     * @Param gpGramWeight
     * @Return TableDataInfo
     */
    @ApiOperation("详情")
    @PreAuthorize("@ss.hasPermi('module:gramweight:query')" )
    @GetMapping(value = "/{id}" )
    public AjaxResult<GpGramWeightVo> getInfo(@PathVariable("id" ) Long id) {
        return AjaxResult.success(iGpGramWeightService.getVoById(id));
    }



    /**
     * @Description 删除
     * @Author gpframework
     * @Date 2023-12-11
     * @Param gpGramWeight
     * @Return TableDataInfo
     */
    @ApiOperation("删除")
    @PreAuthorize("@ss.hasPermi('module:gramweight:remove')" )
    @Log(title = "删除克重" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}" )
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(iGpGramWeightService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }

    /**
     * @Description 复制
     * @Author gpframework
     * @Date 2023-12-11
     * @Param gpGramWeight
     * @Return TableDataInfo
     */
    @ApiOperation("复制")
    @PreAuthorize("@ss.hasPermi('module:gramweight:add')" )
    @Log(title = "复制" , businessType = BusinessType.INSERT)
    @PostMapping("/copy")
    public AjaxResult copy(@Valid @RequestBody GpGramWeightUpdate gpGramWeightUpdate) {
        if (gpGramWeightUpdate == null || gpGramWeightUpdate.getId() == null || gpGramWeightUpdate.getCopyNum() == null) {
            return AjaxResult.parameterError();
        }
        if (gpGramWeightUpdate.getCopyNum() < 1) {
            return AjaxResult.error("复制条数不能小于1");
        }
        return toAjax(iGpGramWeightService.copy(gpGramWeightUpdate.getId(), gpGramWeightUpdate.getCopyNum()) ? 1 : 0);
    }
}
